package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.CourseMapper;
import com.cqjtu.drive.pojo.Course;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Response<Course> getCourseById(String courseId) {
        Course course = courseMapper.getCourseById(courseId);
        if (course != null) {
            return new Response<>(true, "Course found", course);
        } else {
            return new Response<>(false, "Course not found");
        }
    }

    @Override
    public Response<String> insertCourse(Course course) {
        try {
            List<Course> list = courseMapper.getAllCourses();
            if(list != null && !list.isEmpty()){
                String coId = Integer.toString(Integer.parseInt(list.get(list.size() - 1).getCoId()) + 1);
                course.setCoId(coId);
            }else {
                course.setCoId("1");
            }
            courseMapper.insertCourse(course);
            return new Response<>(true, "Course inserted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to insert course: " + e.getMessage());
        }
    }

    @Override
    public Response<String> updateCourse(Course course) {
        try {
            courseMapper.updateCourse(course);
            return new Response<>(true, "Course updated successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to update course: " + e.getMessage());
        }
    }

    @Override
    public Response<String> deleteCourse(String courseId) {
        try {
            courseMapper.deleteCourse(courseId);
            return new Response<>(true, "Course deleted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to delete course: " + e.getMessage());
        }
    }

    @Override
    public Response<List<Course>> getAllCourses() {
        List<Course> courses = courseMapper.getAllCourses();
        if (courses != null && !courses.isEmpty()) {
            return new Response<>(true, "Courses retrieved successfully", courses);
        } else {
            return new Response<>(false, "No courses found");
        }
    }

    @Override
    public Response<List<Course>> getCoursesByname(String coName) {
        List<Course> courses = courseMapper.getCourseByname(coName);
        if (courses != null && !courses.isEmpty()) {
            return new Response<>(true, "Courses retrieved successfully", courses);
        } else {
            return new Response<>(false, "No courses found");
        }
    }

    @Override
    public Response<List<Course>> getBycId(String cId) {
        List<Course> courses = courseMapper.getCourseBycId(cId);
        if (courses != null && !courses.isEmpty()) {
            return new Response<>(true, "Courses retrieved successfully", courses);
        } else {
            return new Response<>(false, "No courses found");
        }
    }


}
